﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ZK.Qymd_TransManage.Com
{
    public class DataToExcel
    {

        /// <summary>
                /// 导出报表为Csv
                /// </summary>
                /// <param name="dt">DataTable</param>
                /// <param name="strFilePath">物理路径</param>
                /// <param name="tableheader">表头</param>
                /// <param name="columname">字段标题,逗号分隔</param>
        public static bool dt2csv(DataTable dt, string strFilePath, string tableheader, string columname)
        {
            try
            {
                string strBufferLine = "";
                StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
                strmWriterObj.WriteLine(tableheader);
                strmWriterObj.WriteLine(columname);
                if (dt.Rows.Count < 1)
                {
                    return false;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strBufferLine = "";
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j > 0)
                            strBufferLine += ",";
                        strBufferLine += dt.Rows[i][j].ToString();
                    }
                    strmWriterObj.WriteLine(strBufferLine);
                }
                strmWriterObj.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }



        /// <summary>
                /// List转DataTable
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <param name="collection"></param>
                /// <returns></returns>
        public static DataTable ToDataTable<T>(IEnumerable<T> collection)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            // dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType) && p.GetCustomAttributes().Contains<>(UsePropertyAttribute)).ToArray());
            /*
             添加表格中有用的列
             使用attribute 属性判断。
              */

            foreach (PropertyInfo pi in props)
            {
                var piListAtt = pi.GetCustomAttributes();
                foreach (Attribute item in piListAtt)
                {
                    if (AttributeFit(item))
                    {
                        dt.Columns.Add(new DataColumn(pi.Name, pi.PropertyType));
                    }
                }

            }

            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        var piListAtt = pi.GetCustomAttributes();
                        foreach (Attribute item in piListAtt)
                        {
                            if (AttributeFit(item))
                            {
                                object obj = pi.GetValue(collection.ElementAt(i), null);
                                if (obj == null)
                                {
                                    switch (pi.PropertyType.Name)
                                    {
                                        case "Double":
                                            tempList.Add(0D);
                                            break;
                                        default:
                                            tempList.Add(null);
                                            break;
                                    }
                                }

                                else
                                {
                                    tempList.Add(obj);
                                }

                                goto a1;
                            }
                        }

                    a1:;
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }

        private static bool AttributeFit(Attribute attribute)
        {
            bool res = false;
            if (attribute.GetType().Name == "UsePropertyAttribute")
            {
                bool resBB = (bool)attribute.GetType().
                                     GetProperty("RES").
                                     GetValue(attribute);//获取attribute中某个属性的值
                res = resBB;
            }
            return res;
        }

    }
}
